package com.dzl.exchange;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.exchange.pojo.WelfareExchange;
import com.dzl.exchange.pojo.WelfareExchangeSecret;
import com.dzl.exchange.pojo.vo.ExchangeMealGoodsVo;
import com.dzl.exchange.pojo.vo.ExchangeSecretVo;
import com.dzl.exchange.pojo.vo.ExchangeSkuGoodsVo;
import com.dzl.exchange.pojo.vo.WelfareExchangeWithSecretVo;
import com.dzl.exchange.service.WelfareExchangeSecretService;
import com.dzl.exchange.service.WelfareExchangeService;
import com.dzl.goods.pojo.vo.WelfareGoodsSupplierPreviewVo;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 兑换券信息表(WelfareExchangeSecretDetails)表控制层
 * @author ChenLong
 * @date 2021-07-10 09:12:52
 */
@Slf4j
@RestController
@RequestMapping("/welfareExchangeSecret")
public class WelfareExchangeSecretController extends BaseController {

    @Autowired
    private WelfareExchangeSecretService service;
    @Autowired
    private WelfareExchangeService welfareExchangeService;


    @GetMapping("getUserExchangeBindList")
    public JsonResult<List<WelfareExchangeWithSecretVo>> getUserExchangeBindList() {
        log.info("getUserExchangeBindList in");
        Integer userId = getWxUserInfo().getId();
        return JsonResult.success(service.getUserExchangeBindList(userId));
    }

    /**
     * 通过券号查询兑换券详细信息
     * @param number 券号
     * @author liuguanjun
     * @date 2021-12-03 14:58:52
     */
    @GetMapping("/queryExchangeSecretByNumber")
    public JsonResult<ExchangeSecretVo> queryExchangeSecretByNumberOrSecret(String number) {
        ExchangeSecretVo vo = service.queryExchangeSecretByNumberOrSecret(number, null);
        return JsonResult.success(vo);
    }

    /**
     * 确认兑换券
     * @param exchangeSecretId 兑换券密id
     * @author liuguanjun
     * @since 2021-12-06 09:24:19
     **/
    @GetMapping(value = "confirmExchangeSecret")
    public JsonResult<String> confirmExchangeSecret(Integer exchangeSecretId) {
        return JsonResult.success(service.confirmExchangeSecret(getWxUserId(),exchangeSecretId));
    }

    /**
     * 获取兑换券选商品规则下的选中的SKU及商品
     * @param exchangeSecretId 兑换券密id
     * @author liuguanjun
     * @date 2021-12-03 10:31:52
     */
    @GetMapping(value = "getExchangeGoodsListByPage")
    public JsonResult<WelfareExchangeWithSecretVo> getExchangeSkuAndGoods(Integer exchangeSecretId) {
        if(EmptyUtils.isEmpty(exchangeSecretId)){
            return JsonResult.paramErrWithMsg("兑换券密id不能传空");
        }
        WelfareExchangeWithSecretVo welfareExchangeWithSecretVo = new WelfareExchangeWithSecretVo();
        WelfareExchangeSecret welfareExchangeSecret = service.selectByPrimaryKey(exchangeSecretId);
        if(EmptyUtils.isEmpty(welfareExchangeSecret)){
            return JsonResult.failWithMsg("兑换券密不存在");
        }
        WelfareExchange welfareExchange = welfareExchangeService.selectByPrimaryKey(welfareExchangeSecret.getGroupId());
        if(EmptyUtils.isEmpty(welfareExchange)){
            return JsonResult.failWithMsg("兑换券不存在");
        }
        List<ExchangeSkuGoodsVo> voList = welfareExchangeService.getExchangeSkuAndGoods(welfareExchangeSecret.getGroupId());
        BeanUtils.copyProperties(welfareExchangeSecret,welfareExchangeWithSecretVo);
        welfareExchangeWithSecretVo.setGroupName(welfareExchange.getName());
        welfareExchangeWithSecretVo.setBuyLimit(welfareExchange.getBuyLimit());
        welfareExchangeWithSecretVo.setRuleType(welfareExchange.getRuleType());
        welfareExchangeWithSecretVo.setExchangeGoodsList(voList);
        return JsonResult.success(welfareExchangeWithSecretVo);
    }


    /**
     * 获取兑换券关联的套餐及套餐下的SKU及商品
     * @param exchangeSecretId 兑换券密id
     * @author liuguanjun
     * @date 2021-12-03 10:20:52
     */
    @GetMapping(value = "getExchangeMealGoodsListByPage")
    public JsonResult<WelfareExchangeWithSecretVo> getExchangeMealCheckedSkuAndGoods(Integer exchangeSecretId) {
        if(EmptyUtils.isEmpty(exchangeSecretId)){
            return JsonResult.paramErrWithMsg("兑换券密id不能传空");
        }
        WelfareExchangeWithSecretVo welfareExchangeWithSecretVo = new WelfareExchangeWithSecretVo();
        WelfareExchangeSecret welfareExchangeSecret = service.selectByPrimaryKey(exchangeSecretId);
        if(EmptyUtils.isEmpty(welfareExchangeSecret)){
            return JsonResult.failWithMsg("兑换券密不存在");
        }
        WelfareExchange welfareExchange = welfareExchangeService.selectByPrimaryKey(welfareExchangeSecret.getGroupId());
        if(EmptyUtils.isEmpty(welfareExchange)){
            return JsonResult.failWithMsg("兑换券不存在");
        }
        List<ExchangeMealGoodsVo> voList = welfareExchangeService.getExchangeMealCheckedSkuAndGoods(welfareExchangeSecret.getGroupId());
        BeanUtils.copyProperties(welfareExchangeSecret,welfareExchangeWithSecretVo);
        welfareExchangeWithSecretVo.setGroupName(welfareExchange.getName());
        welfareExchangeWithSecretVo.setBuyLimit(welfareExchange.getBuyLimit());
        welfareExchangeWithSecretVo.setRuleType(welfareExchange.getRuleType());
        welfareExchangeWithSecretVo.setExchangeMealList(voList);
        return JsonResult.success(welfareExchangeWithSecretVo);
    }


    /**
     * 根据套餐Id获取套餐商品列表
     * @param mealId 兑换券套餐id
     * @author ChenLong
     * @since 2021-05-20 10:27:19
     **/
    @GetMapping(value = "getExchangeMealGoodsListByMealId")
    public JsonResult<List<ExchangeSkuGoodsVo>> getExchangeMealGoodsListByMealId(Integer mealId) {
        List<ExchangeSkuGoodsVo> list = welfareExchangeService.getExchangeMealCheckedSkuAndGoodsByMealId(mealId);
        return JsonResult.success(list);
    }

    /**
     * 根据兑换券组获取套餐商品列表
     * @param param type 1兑换券商品 2兑换券套餐
     * @param param itemIds 选中的套餐或商品id,中间逗号分隔
     * @author ChenLong
     * @since 2021-05-20 10:27:19
     **/
    @PostMapping(value = "getGoodsGroupSupplierBySkuList")
    public JsonResult<WelfareGoodsSupplierPreviewVo> getGoodsGroupSupplierBySkuList(@RequestBody JSONObject param) {
        //log.info("getGoodsGroupSupplierBySkuList in dto: " + dto.toString());
        Integer type = param.getInteger("type");
        String itemIds = param.getString("itemIds");
        return JsonResult.success(welfareExchangeService.getOrderItemsPreview(type,itemIds));
    }
}
